// 路由守卫
import router from './index'
import { useUserStore } from '@/stores/user'
import { roleStore } from '@/stores/role'
import { mapStore } from '@/stores/worldMap'
import { headersStore } from '@/stores/apiHeader'
import { downloadLanguage } from '@/i18n'
import { useHomeStore } from '@/stores/module/home'
import { useFinanceStore } from '@/stores/module/finance'
import { useSupplyStore } from '@/stores/module/supply'
import { useDealerDataStore } from '@/stores/module/dealerData'
// 路由白名单
const whiteList = ['/login', '/404', '/dealer-data']
const reg = /test/ // test页统一过
router.beforeEach(async (to, from, next) => {
  // map里面的initTime
  mapStore().setInitTime(5000)
  // 上来加载语言 (如果是/不加载)
  if (to.path !== '/') {
    downloadLanguage()
  }
  // 判断是否在白名单
  if (whiteList.indexOf(to.path) !== -1 || reg.test(to.path)) {
    // 登录
    to.path === '/login' && loginFunc(to, from, next)
    // 经销商
    to.path === '/dealer-data' && dealerFunc(to, from, next)
    next()
  } else {
    // 判断是否有token
    if (useUserStore().token) {
      if(to.path === '/'){
        indexFunc(to, from, next)
        next()
        return ''
      }
      const headerStore = headersStore()
      headerStore.setDistributorKey('') // 清空经销商key
      nextFunc(to, from, next) // 子页面方法
      // 设置年
      if (!headerStore.year) {
        headerStore.setYear(new Date().getFullYear().toString())
      }
      next()
    } else {
      next('/login')
    }
  }
})
// 登录页面路由守卫
const loginFunc = (to: any, from: any, next: any) => {
  // 清除用户信息,角色信息,header信息
  useUserStore().clear()
  roleStore().clear()
  mapStore().clear()
  // 获取当前语言
  const headerStore = headersStore()
  const language_id = headerStore.language_id
  headerStore.clear()
  headerStore.setLanguageId(language_id)
  // 清除home,finance,supply的pinia
  useHomeStore().$reset()
  useFinanceStore().$reset()
  useSupplyStore().$reset()
}
// 主页的路由守卫,根据角色分配页面
const indexFunc = (to: any, from: any, next: any) => {
  // 获取角色和头部信息
  const { businessLevel, supplyLevel, financeLevel, humanLevel } = roleStore()
  const headerStore = headersStore()
  let typeId = '1'
  humanLevel && (typeId = '5')
  financeLevel && (typeId = '3')
  supplyLevel && (typeId = '2')
  businessLevel && (typeId = '1')

  // 设置typeId并进行下一步操作
  headerStore.setTypeId(typeId)
  const nextMap = {
    '1': '/home',
    '2': '/supply',
    '3': '/finance',
    '5': '/human'
  }
  const nextPath = nextMap[typeId]
  next(nextPath)
}
// 子页面方法 (home,finance,supply,human)
const nextFunc = (to: any, from: any, next: any) => {
  const userStore: any = useUserStore()
  const rolesStore = roleStore()
  const headerStore = headersStore()
  // 获取到path
  const path = to.path
  //path和typeId的对应关系
  const pathMap = {
    '/home': '1',
    '/supply': '2',
    '/finance': '3',
    '/human': '5'
  }
  // 设置typeId
  const typeId = pathMap[path] || '1'
  headerStore.setTypeId(typeId)
  // 设置scopeCode和pointCode
  const scopeCodeMap = {
    '1': 'bussinessCode',
    '2': 'supplyCode',
    '3': 'financeCode',
    '5': 'humanCode'
  }
  const scopeCodeArr = userStore.userInfo[scopeCodeMap[typeId]] || []
  const scopeCode = scopeCodeArr&& scopeCodeArr.length>0 ? scopeCodeArr[0] : '0'
  headerStore.setScopeCode(scopeCode)
  headerStore.setPointCode(scopeCodeArr)
  // 设置角色等级
  const levelMap = {
    '1': userStore.userInfo.businessLevel,
    '2': userStore.userInfo.supplyLevel,
    '3': userStore.userInfo.financeLevel,
    '5': userStore.userInfo.humanLevel
  }
  const level = levelMap[typeId]
  // 根据typeId的不同,设置不同的角色等级
  const roleFuncMap = {
    '1': 'setBusinessLevel',
    '2': 'setSupplyLevel',
    '3': 'setFinanceLevel',
    '5': 'setHumanLevel',
  }
  // 设置角色等级
  const roleFunc = roleFuncMap[typeId]
  rolesStore[roleFunc](Number(level))
}
// 经销商页面路由守卫
const dealerFunc = (to: any, from: any, next: any) => {
  // 获取经销商id参数
  const uid = to.query.uid
  // 如果没参数直接跳转到登录页
  if (!uid) {
    next('/login')
  }
  const headerStore = headersStore()
  // 判断当前id和header中的id是否一致
  if (headerStore.distributor_key != uid) {
    // 清除dealerData的pinia
    useDealerDataStore().$reset()
  }
  // 存储uid
  headerStore.setDistributorKey(uid)
  // 设置TypeId
  headerStore.setTypeId('4')
  // 设置年
  if (!headerStore.year) {
    headerStore.setYear(new Date().getFullYear().toString())
  }
}